package com.example.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description: TODO
 * @Author: Van.Planifolia
 * @CreateTime: 2022-12-26  20:12
 * @Version: 1.0
 */
@Configuration
public class ElasticSearchConfig {
    /**
     * 在配置类中配置并且注册这个RestHighLevelClient类
     * @return 初始化完毕的 RestHighLevelClient
     */
    @Bean
    public RestHighLevelClient restHighLevelClient(){
        // 因为RestClient的builder方法中需要一个HttpHost对象，我们就要给他new一个，并且这个对象中初始化的信息正是我们的es服务器地址
        HttpHost host = new HttpHost("127.0.0.1", 9200, "http");
        // 因为i注册RestHighLevelClient需要RestClientBuilder对象，我们就使用RestClient工具类创建一个
        RestClientBuilder restClientBuilder = RestClient.builder(host);
        // 注册RestHighLevelClient对象
        return new RestHighLevelClient(restClientBuilder);
    }
}
